package com.itheima.service.impl;

import com.itheima.mapper.SurveyMapper;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Survey;
import com.itheima.service.SurveyService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

import static java.lang.Integer.parseInt;
public class SurveyServiceImpl implements SurveyService {

    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public List<Survey> selectAll() {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取SurveyMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);

        //4. 调用方法
        List<Survey> surveys = mapper.selectAll();

        //5. 释放资源
        sqlSession.close();

        return surveys;
    }

    @Override
    public void add(Survey survey) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取SurveyMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);

        //4.调用方法
        mapper.add(survey);
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);

        //4. 调用方法
        mapper.deleteByIds(ids);

        sqlSession.commit();//提交事务

        //5. 释放资源
        sqlSession.close();
    }

    @Override
    public void delete(String id) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取SurveyMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);

        if(id !=null && !id .equals("")){
            //4. 调用方法
            mapper.delete(Integer.parseInt(id));

        }



        sqlSession.commit();//提交事务

        //5. 释放资源
        sqlSession.close();

    }

    @Override
    public PageBean<Survey> selectByPage(int currentPage, int pageSize,int creatorId) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        //5. 查询当前页数据
        List<Survey> rows = mapper.selectByPage(begin, size,creatorId);

        //6. 查询总记录数
        int totalCount = mapper.selectTotalCount(creatorId);

        //7. 封装PageBean对象
        PageBean<Survey> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Survey> MselectByPage(int currentPage, int pageSize) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        //5. 查询当前页数据
        List<Survey> rows = mapper.MselectByPage(begin, size);

        //6. 查询总记录数
        int totalCount = mapper.MselectTotalCount();

        //7. 封装PageBean对象
        PageBean<Survey> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Survey> selectByPageAndCondition(int currentPage, int pageSize, Survey survey) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        // 处理brand条件，模糊表达式
        String surveyName = survey.getSurveyName();
        if (surveyName != null && surveyName.length() > 0) {
            survey.setSurveyName("%" + surveyName + "%");
        }

        String surveyDescription = survey.getSurveyDescription();
        if (surveyDescription != null && surveyDescription.length() > 0) {
            survey.setSurveyDescription("%" + surveyDescription + "%");
        }


        //5. 查询当前页数据
        List<Survey> rows = mapper.selectByPageAndCondition(begin, size, survey);

        //6. 查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(survey);

        //7. 封装PageBean对象
        PageBean<Survey> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }

    public PageBean<Survey> selectBystatus(int currentPage, int pageSize){
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        SurveyMapper mapper = sqlSession.getMapper(SurveyMapper.class);

        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        //6. 查询总记录数
        int totalCount = mapper.MselectTotalCount();
        List<Survey> rows = mapper.selectByStatus(begin,size);
        //System.out.println(rows);

        //7. 封装PageBean对象
        PageBean<Survey> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        //5. 释放资源
        sqlSession.close();

        return pageBean;
    }


}
